package com.jifujun.junbatis.base.service;


import com.jifujun.junbatis.base.dao.GenericDao;
import com.jifujun.junbatis.base.model.RootObject;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * Create with IntelliJ IDEA
 * Project Name: junbatis
 * Package Name: club.emergency.junbatis.base.club.emergency.staff.service.impl
 * Date: 2018/3/30 08:19
 * User: 快乐的小菌菌
 * Description: 通用Service实现类 所有的Service实现类统一继承本类
 */
@Slf4j
public class GenericServiceImpl<ID, T extends RootObject> implements GenericService<ID, T> {

    private GenericDao<ID, T> genericDao;

    private Class<T> clazz;

    public GenericServiceImpl(GenericDao<ID, T> genericDao, Class<T> clazz) {
        this.genericDao = genericDao;
        this.clazz = clazz;
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return T t
     */
    @Override
    public T getById(ID id) {
        return genericDao.getById(id, clazz);
    }

    /**
     * 查询列表，没有分页
     *
     * @param conditionConstructor 查询条件
     * @return list
     */
    @Override
    public List<T> selectList(ConditionConstructor conditionConstructor) {
        return genericDao.selectListForOne(
                conditionConstructor.selectConditionCompleteToArray(),
                conditionConstructor.getSelectConditionContainer(),
                conditionConstructor.orConditionCompleteToArray(),
                conditionConstructor.getOrConditionContainer(),
                null,
                null,
                conditionConstructor.getOrderByArgs(),
                false,
                clazz);
    }

    /**
     * 分页查询
     *
     * @return 返回列表
     */
    @Override
    public PageResult selectInPage(Page page) {
        //分页数据数量
        Integer count = genericDao.count(
                page.selectConditionCompleteToArray(),
                page.getSelectConditionContainer(),
                page.orConditionCompleteToArray(),
                page.getOrConditionContainer(),
                clazz);
        //分页返回List
        List<T> inPage = genericDao.selectInPageForOne(
                page.selectConditionCompleteToArray(),
                page.getSelectConditionContainer(),
                page.orConditionCompleteToArray(),
                page.getOrConditionContainer(),
                page.getOffset(),
                page.getPageSize(),
                page.getOrderByArgs(),
                true,
                clazz);

        PageResult pageResult = new PageResult();
        page.setTotalCount(count);
        page.setTotalPage((int) Math.ceil((double) count / (double) page.getPageSize()));
        pageResult.setPageInfo(page);
        pageResult.setList(inPage);
        return pageResult;
    }

    public List<T> selectListForMore() {
        return genericDao.selectListForMore(clazz);
    }

    /**
     * 新增数据
     *
     * @param t object
     */
    @Override
    public void insertOne(T t) {
        t.setUsingType(1);
        genericDao.insertOne(t);
    }

    /**
     * 批量insert数据，具体使用
     * {@link ConditionConstructor#insertMoreConstructor(List)}
     * 以List形式入参。
     *
     * @param conditionConstructor ConditionConstructor对象()
     */
    @Override
    public void insertMore(ConditionConstructor conditionConstructor) {
        genericDao.insertMore(conditionConstructor.insertMoreCompleteToArray(clazz), conditionConstructor.getInsertMoreContainer(), clazz);
    }

    /**
     * 根据id删除数据
     *
     * @param id 数据id
     */
    @Override
    public void deleteById(ID id) {
        genericDao.deleteById(id, clazz);
    }

    /**
     * 根据条件删除
     *
     * @param conditionConstructor ConditionConstructor对象
     */
    @Override
    public void deleteByCondition(ConditionConstructor conditionConstructor) {
        genericDao.deleteByCondition(conditionConstructor.deleteConditionCompleteToArray(), conditionConstructor.getDeleteConditionContainer(), clazz);
    }

    /**
     * 根据id逻辑删除
     *
     * @param id id
     */
    @Override
    public void removeById(ID id) {
        genericDao.removeById(id, clazz);
    }

    /**
     * 更新数据
     * id直接放在new出来的对象里即可
     *
     * @param t object
     */
    @Override
    public void updateById(ID id, T t) {
        ConditionConstructor conditionConstructor = new ConditionConstructor();
        conditionConstructor.conditionUpdate(id, Condition.EQUAL, "id");
        conditionConstructor.setUpdateConditionObject(t);
        updateByCondition(conditionConstructor);
    }

    /**
     * 根据条件更新
     *
     * @param conditionConstructor ConditionConstructor对象
     */
    @Override
    public void updateByCondition(ConditionConstructor conditionConstructor) {
        genericDao.updateByCondition(conditionConstructor.updateConditionCompleteToArray(), conditionConstructor.getUpdateConditionContainer(), conditionConstructor.getUpdateConditionObjectMap(), conditionConstructor.updateValueCompleteToArray(), clazz);
    }
}
